package com.test.deencredit.activity_deencredit.ln_deencredit;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.deencredit.R;
import com.test.deencredit.base_deencredit.BaseActivity_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.LnAplyDetailInfo_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.LnAplyItem_deencredit;
import com.test.deencredit.dialog_deencredit.CustomerServiceDialog_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

import java.util.ArrayList;

public class LnAplyActivity_deencredit extends BaseActivity_deencredit {

    private TextView currencyTopTv_deencredit, amountTopTv_deencredit, termTopTv_deencredit;
    private LinearLayout pGroupLayout_deencredit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ln_aply_deencredit);
        page_deencredit = "APPLY";

        SwipeRefreshLayout refreshLayout = findViewById(R.id.ala_refreshview);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            HttpUtil_deencredit.getInstance_deencredit().requestLnAplyDetail_deencredit(this,this::onAplyDetailResp_deencredit);
        });

        findViewById(R.id.ala_ib_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.ala_ib_customerservice).setOnClickListener(v -> new CustomerServiceDialog_deencredit(this).show());

        currencyTopTv_deencredit = findViewById(R.id.ala_tv_currencytop);
        amountTopTv_deencredit = findViewById(R.id.ala_tv_amounttop);
        termTopTv_deencredit = findViewById(R.id.ala_tv_termtop);
        pGroupLayout_deencredit = findViewById(R.id.ala_layout_param_group);


        findViewById(R.id.ala_btn_submit).setOnClickListener(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"SUBMIT_CLICK","");
            HttpUtil_deencredit.getInstance_deencredit().aply_deencredit(this,this::onLnAplyResp_deencredit);
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpUtil_deencredit.getInstance_deencredit().requestLnAplyDetail_deencredit(this,this::onAplyDetailResp_deencredit);
    }

    @SuppressLint("SetTextI18n")
    public void onAplyDetailResp_deencredit(){

        String amount = LnAplyDetailInfo_deencredit.getInstance_deencredit().getAmount_deencredit();
        String currency = LnAplyDetailInfo_deencredit.getInstance_deencredit().getCurrency_deencredit();

        amountTopTv_deencredit.setText(amount);
        currencyTopTv_deencredit.setText(currency);
        termTopTv_deencredit.setText(LnAplyDetailInfo_deencredit.getInstance_deencredit().getTermStr_deencredit(this));

        refreshParamView_deencredit();
    }

    @SuppressLint("InflateParams")
    private void refreshParamView_deencredit(){

        pGroupLayout_deencredit.removeAllViews();

        ArrayList<LnAplyItem_deencredit> params = LnAplyDetailInfo_deencredit.getInstance_deencredit().getDetailItemList_deencredit();

        for (int i=0;i<params.size();i++){

            LnAplyItem_deencredit param = params.get(i);

            if (param == null) continue;

            View view;
            TextView pNameTv;
            TextView pValueTv;

            if (param.isTitle_deencredit()){
                if (i>0) pGroupLayout_deencredit.addView(LayoutInflater.from(this).inflate(R.layout.item_lnaply_divider_deencredit,null));
                view = LayoutInflater.from(this).inflate(R.layout.item_lnaply_title_deencredit,null);
                pNameTv = view.findViewById(R.id.ilt_tv_p_name);
                pValueTv = view.findViewById(R.id.ilt_tv_p_value);
            }else {
                view = LayoutInflater.from(this).inflate(R.layout.item_lnaply_detail_deencredit,null);
                pNameTv = view.findViewById(R.id.ild_tv_p_name);
                pValueTv = view.findViewById(R.id.ild_tv_p_value);
            }

            pNameTv.setText(param.getShowName_deencredit());
            pValueTv.setText(param.getShowDetail_deencredit());

            pGroupLayout_deencredit.addView(view);

        }

    }

    public void onLnAplyResp_deencredit(){
        HttpUtil_deencredit.getInstance_deencredit().userProcess_deencredit(this,this::onProcessResp_deencredit);
    }

    public void onProcessResp_deencredit(){
        Intent intent = new Intent(this, MainActivity_deencredit.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

}